package com.telit.firecommand.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telit.android.dto.CommMsgCenterLogDTO;
import com.telit.common.constant.DictTypeConstants;
import com.telit.common.exception.ForestBaseException;
import com.telit.dispose.vo.resourceManagement.DictVO;
import com.telit.firecommand.dao.CommMsgCenterLogDao;
import com.telit.firecommand.dao.DictMapper;
import com.telit.firecommand.dto.CommMsgCenterLog;
import com.telit.firecommand.dto.QueryMsgCenterDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Auther amy
 * @Describe 消息中心
 */
@Service
public class MsgCenterService extends ServiceImpl<CommMsgCenterLogDao, CommMsgCenterLog> {

    @Autowired
    private DictMapper dictMapper;

    /**
     * 分页查询数据
     *
     * @param current
     * @param size
     *
     * @return
     */
    public IPage<CommMsgCenterLog> listByPage(Integer current, Integer size, QueryMsgCenterDTO queryMsgCenterDTO) {
        IPage<CommMsgCenterLog> commEventPage = new Page<>(current, size);
        return baseMapper.listByCondition(commEventPage, queryMsgCenterDTO);
    }

    /**
     * 查询详情
     *
     * @param id
     *
     * @return
     */
    public CommMsgCenterLogDTO detail(Long id) {
        CommMsgCenterLog commMsgCenterLog = baseMapper.selectById(id);
        if (commMsgCenterLog == null) {
            throw new ForestBaseException("该消息详情不存在");
        }
        CommMsgCenterLogDTO dto = new CommMsgCenterLogDTO();
        BeanUtils.copyProperties(commMsgCenterLog, dto);

        List<DictVO> dictVOS = dictMapper.listByParentCode(DictTypeConstants.DICT_TYPE_MSG_PUSH_TYPE);
        Map<String, String> map = dictVOS.stream().collect(
                Collectors.toMap(DictVO::getCode, DictVO::getName, (e1, e2) -> e1));
        dto.setMsgTypeDesc(map.get(commMsgCenterLog.getMsgType()));

        List<DictVO> dictVOList = dictMapper.listByParentCode(DictTypeConstants.DICT_TYPE_MSG_PUSH_RESOURCE);
        Map<String, String> map2 = dictVOList.stream().collect(
                Collectors.toMap(DictVO::getCode, DictVO::getName, (e1, e2) -> e1));
        dto.setMsgResourceDesc(map2.get(commMsgCenterLog.getMsgResource()));
        return dto;
    }

    /**
     * 新增消息
     *
     * @return
     */
    public Long add(CommMsgCenterLog commMsgCenterLog) {
        if (baseMapper.insert(commMsgCenterLog) <= 0) {
            throw new ForestBaseException("新增通知中心消息失败");
        }
        return commMsgCenterLog.getId();
    }

    /**
     * 更新消息状态
     *
     * @param ids
     * @param status
     *
     * @return
     */
    public int updateStatus(List<Long> ids, String status) {
        return baseMapper.updateStatusBatch(status, ids);
    }

    /**
     * 批量添加
     *
     * @return
     */
    @Transactional
    public void addBatch(CommMsgCenterLogDTO dto) {
        if (dto != null && !CollectionUtils.isEmpty(dto.getUserIdList())) {
            List<CommMsgCenterLog> resultList = new ArrayList<>();
            List<Long> userList = dto.getUserIdList();
            userList.forEach(userId -> {
                CommMsgCenterLog log = new CommMsgCenterLog();
                BeanUtils.copyProperties(dto, log);
                log.setUserId(userId);
                resultList.add(log);
            });
            if (baseMapper.addBatch(resultList) <= 0) {
                throw new ForestBaseException("批量新增消息失败");
            }
        }
    }

    /**
     * 未读信息条数
     *
     * @param queryMsgCenterDTO
     *
     * @return
     */
    public int unReadCount(QueryMsgCenterDTO queryMsgCenterDTO) {
        return baseMapper.listCount(queryMsgCenterDTO);
    }

}
